﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.17
 * 描 述：支票
 */
var refreshGirdData; // 更新数据
var selectedRow;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                selectedRow = null;
                learun.layerForm({
                    id: 'Form',
                    title: '添加',
                    url: config.webportal_server + 'pages/bill/yhzp/form.html?access_token=' + config.getToken().access_token,
                    width: 750,
                    height: 500,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'Form',
                        title: '编辑',
                        url: config.webportal_server + 'pages/bill/yhzp/form2.html?access_token=' + config.getToken().access_token,
                        width: 1300,
                        height: 550,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'ghzps/delete/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 数据导出
            $('#lr_export').on('click', function () {
                location.href = config.base_server() + 'ghzps/export?access_token=' + config.getToken().access_token;
            });

            // 打印
            $('#lr_print').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    var hiprintTemplate = null;
                    learun.httpAsyncGet2(config.base_server() + 'printTempletes/findByCode', '&code=icbc_ghzp', function (res) {
                        if (res.resp_code == 200) {
                            hiprintTemplate = new hiprint.PrintTemplate({
                                template: JSON.parse(res.data.templete),
                                settingContainer: '#PrintElementOptionSetting',
                                paginationContainer: '.hiprint-printPagination'
                            });

                            learun.httpAsyncGet2(config.base_server() + 'ghzps/findById', "&id=" + keyValue, function (res) {
                                if (res.resp_code == 200) {
                                    hiprintTemplate.resetPage();
                                    hiprintTemplate.print(res.data);
                                }
                            });
                        }
                    });
                }
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'ghzps/list?access_token=' + config.getToken().access_token,
                headData: [
                    {
                        label: "出票日期", name: "djrq", width: 120, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd');
                        }
                    },
                    {
                        label: "收款人", name: "skr", width: 200, align: 'center',
                    },
                    {
                        label: "金额", name: "金额", width: 200, align: 'center',
                        children: [
                            {
                                label: "大写", name: "money1", width: 100, align: "left"
                            },
                            {
                                label: "小写", name: "money2", width: 100, align: "left",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            }
                        ]
                    },
                    {label: "用途", name: "useStr", width: 120, align: "left"},
                    {label: "密码", name: "passwordStr", width: 60, align: "left"},
                    {label: "复核", name: "fh", width: 100, align: "left"},
                    {label: "记账", name: "jz", width: 100, align: "left"},
                    {label: "财务主管", name: "cwzg", width: 100, align: "left"},
                    {label: "会计", name: "kj", width: 100, align: "left"},
                    {label: "附加信息", name: "remark", width: 150, align: "center"},
                    {
                        label: "创建时间", name: "createTime", width: 140, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {label: "创建人", name: "createUsername", width: 80, align: "left"},
                ],
                dblclick: function (row) {
                    var keyValue = $('#gridtable').jfGridValue('id');
                    selectedRow = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'Form',
                            title: '编辑',
                            url: config.webportal_server + 'pages/bill/yhzp/form.html?access_token=' + config.getToken().access_token,
                            width: 750,
                            height: 500,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                isTree: false,
                mainId: 'id',
                isPage: true,
                reloadSelected: true,
            });
            page.search();
        },
        search: function (param) {
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    }

    page.init();
}


